package org.yanghe.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.yanghe.myhandler.CustomerBlockHandler;

/**
 * @SentinelResource 注解使用
 */
@RestController
public class RateLimitController {

    @GetMapping(value = "/byResource")
    @SentinelResource(value = "byResource",blockHandler = "handlerException")
    public String byResource(){
        return "200, 按资源名称限流测试OK!";
    }

    /**
     * 兜底方法
     * @param e
     * @return
     */
    public String handlerException(BlockException e){
        return "444, "+e.getClass().getCanonicalName()+"\t 服务不可用!";
    }

    //如果控制台 资源名使用 /byResourceUrl 报错信息默认提示 Blocked by Sentinel (flow limiting)
    @GetMapping(value = "/byUrl")
    @SentinelResource(value = "byResource")
    public String byUrl(){
        return "200, 按 URL 限流测试OK!";
    }

    //=================================================

    /**
     * 配置全局 处理类
     * @return
     */
    @GetMapping(value = "/rateLimit/customerBlockHandler")
    @SentinelResource(value = "customerBlockHandler",
            blockHandlerClass = CustomerBlockHandler.class,
            blockHandler = "handlerException2")
    public String customerBlockHandler(){
        return "200 , 按用户自定义成功访问";
    }
}
